<?php

class CMRPage
{
	
// constants
	protected $NeededForApproval	= 3;
	
	protected $Authenticator;
	protected $Database;

/*	protected $URL;*/
	protected $StyleSheet = "../css/style.css";

	protected $SessionID;
	protected $User;
	protected $UserID;
	protected $Title = "Cathedral Music Resources";
/*
	protected $Group;
*/
	protected $Action = "";
	protected $subAction = "";
	protected $title = "Cathedral Music Resources";
	protected $rightColumnWidth = 250;
	protected $headerImage;
	protected $rightImage1;
	protected $contentHTML;
	protected $contentClass;
	
	protected $isLoggedIn = false;
	protected $userGroup = 0;
	
	public function __construct()
	{
//		echo "Action=".$this->Action;
		if(isset($_POST['Action']))
		{
			$this->Action = $_POST['Action'];
		}
		elseif(isset($_GET['Action']))
		{
			$this->Action = $_GET['Action'];
		}
		
		if(isset($_POST['SubAction']))
		{
			$this->subAction = $_POST['SubAction'];
		}
		elseif(isset($_GET['SubAction']))
		{
			$this->subAction = $_GET['SubAction'];
		}
		
	}

	public function setRightColumnWidth($width)
	{
		$this->rightColumnWidth = $width;
	}

	public function setRightImage1($image)
	{
		$this->rightImage1 = $image;
	}

	public function setHeaderImage($image)
	{
		$this->headerImage = $image;
	}

	public function setContentHTML($content)
	{
//		echo "Content = ".$content;
		$this->contentHTML = $content;
	}

	public function setContentClass($content)
	{
//		echo "Content = ".$content;
		$this->contentClass = $content;
	}

	public function getAction()
	{
		return $this->Action;
	}

	public function getSubAction()
	{
		return $this->subAction;
	}

	public function getHTML()
	{
		if ($this->isAuthenticated())
		{
//			echo "authent";
			$this->setIsLoggedIn(true);
			$sql = "SELECT m.CMRUserID, m.CMRUserGroupID, u.CMRUserID, g.GroupName
				FROM 
				(CMRUserGroupMembers AS m
				INNER JOIN CMRUsers AS u ON m.CMRUserID = u.CMRUserID)
				INNER JOIN CMRUserGroups as g ON g.CMRUserGroupID = m.CMRUserGroupID
				WHERE u.CMRUserID = ".$this->UserID;
		
			$this->Database->ExecuteSQL($sql);
			$row = $this->Database->getRow();
			$this->setUserGroup($row['GroupName']);
			
		}

		
		
		$html = $this->getHeaderHTML();
		$html .= $this->getBodyTopHTML();
		if($this->getIsLoggedIn())
			$html .= $this->getContents();
		else
			$html .= $this->getLogon();
		
		$html .= $this->getRightColumn();
		$html .= $this->getBodyBottomHTML();
		return $html;
	}
	
	protected function setIsLoggedIn($isLoggedIn)
	{
		$this->isLoggedIn = $isLoggedIn;
	}
	
	protected function getIsLoggedIn()
	{
		return $this->isLoggedIn;
	}

	protected function setUserGroup($userGroup)
	{
		$this->userGroup = $userGroup;
	}
	
	protected function getUserGroup()
	{
		return $this->userGroup;
	}

	protected function getHeaderHTML()
	{
		$html = "<html>\n";
		$html .= "<head>\n";
//		$html .= "<style type=\"text/css\">\n";
//		$html .= "body { behavior:url(\"css/csshover.htc\")}\n";
//		$html .= "</style>\n";
		$html .= "<title>".$this->title."</title>\n";
		$html .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$this->StyleSheet."\" />\n";
		return $html;
	}

	protected function getmenuHTML()
	{
		$input = new Hidden();
		$input->setName("Action");
		$input->setValue($this->getAction());
		$html = $input->getHTML();
		$html .= "<script language=\"javascript\">\n";
		$html .= "function setHover(object)\n";
		$html .= "		{\n";
		$html .= "			(document.getElementById(object)).className=\"mainmenuitemhover\";\n";
		$html .= "		}\n";
		$html .= "function unsetHover(object)\n";
		$html .= "		{\n";
		$html .= "			(document.getElementById(object)).className=\"mainmenuitem\";\n";
		$html .= "		}\n";
		$html .= "</script>\n";

		$html .= "<p class=\"mainmenuitem\" id=\"menuHome\" ";
		$html .= "onmouseOver='setHover(\"menuHome\");' \n";
		$html .= "onmouseOut='unsetHover(\"menuHome\");' \n";
		$html .= "onclick='document.getElementById(\"Action\").value =\"home\"; document.getElementById(\"pageForm\").submit();'>\n";
		$html .= "Home";
		$html .= "</p>\n";

		$userGroup = $this->getUserGroup();
		if	(
			(!strcmp($userGroup,"Subscribers"))||
			(!strcmp($userGroup,"Contributors"))||
			(!strcmp($userGroup,"Editors"))||
			(!strcmp($userGroup,"Supervisors"))
			)
		{
			$html .= "<p class=\"mainmenuitem\" id=\"menuResources\" ";
			$html .= "onmouseOver='setHover(\"menuResources\");' \n";
			$html .= "onmouseOut='unsetHover(\"menuResources\");' \n";
			$html .= "onclick='document.getElementById(\"Action\").value =\"resources\"; document.getElementById(\"pageForm\").submit();'>\n";
			$html .= "Search Resources";
			$html .= "</p>\n";
		}

		if	(
			(!strcmp($userGroup,"Contributors"))||
			(!strcmp($userGroup,"Editors"))||
			(!strcmp($userGroup,"Supervisors"))
					)
		{
			$html .= "<p class=\"mainmenuitem\" id=\"menuSubmit\" ";
			$html .= "onmouseOver='setHover(\"menuSubmit\");' \n";
			$html .= "onmouseOut='unsetHover(\"menuSubmit\");' \n";
			$html .= "onclick='document.getElementById(\"Action\").value =\"submit\"; document.getElementById(\"pageForm\").submit();'>\n";
			$html .= "Submit a Resource";
			$html .= "</p>\n";
		}

		if	(
			(!strcmp($userGroup,"Editors"))||
			(!strcmp($userGroup,"Supervisors"))
					)
		{
			$html .= "<p class=\"mainmenuitem\" id=\"menuApprove\" ";
			$html .= "onmouseOver='setHover(\"menuApprove\");' \n";
			$html .= "onmouseOut='unsetHover(\"menuApprove\");' \n";
			$html .= "onclick='document.getElementById(\"Action\").value =\"approve\"; document.getElementById(\"pageForm\").submit();'>\n";
			$html .= "Approve Resources";
			$html .= "</p>\n";
		}

		if($this->getIsLoggedIn())
		{
			$html .= "<p class=\"mainmenuitem\" id=\"menuLogoff\" ";
			$html .= "onmouseOver='setHover(\"menuLogoff\");' \n";
			$html .= "onmouseOut='unsetHover(\"menuLogoff\");' \n";
			$html .= "onclick='document.getElementById(\"Action\").value =\"logoff\"; document.getElementById(\"pageForm\").submit();'>\n";
			$html .= "Log off";
			$html .= "</p>\n";
		}
		
		return $html;

	}

	protected function getRightColumn()
	{	
		$html = "<td width=\"".$this->rightColumnWidth."\">\n";
		$html .= "<a href=\"http://www.cathedralmusicresources.org\"><img src=\"".$this->rightImage1."\" width=\"".$this->rightColumnWidth."\"></a>\n";
		$html .= "</td>\n";
		return $html;
	}

	protected function getBodyTopHTML()
	{
		$html = "</head>\n";
		$html .= "<body>\n";
		$POSTVariables = new PostVariables();
		$html .= $POSTVariables->getHTML();
		$html .= "<form id=\"pageForm\" action=\"home.php\" method = \"POST\">\n";
		$html .= "<input type=\"hidden\" name=\"SessionID\" value=\"".$this->SessionID."\" />\n";
		$html .= "<input type=\"hidden\" name=\"User\" value=\"".$this->User."\" />\n";
		$html .= "<input type=\"hidden\" name=\"UserID\" value=\"".$this->UserID."\" />\n";
		$html .= "<table align=\"center\" width=\"1100\">\n";
//		$html .= "<tr><td class=\"cathwebsite\"><p class=\"cathwebsite\"><a href=\"http://www.stnicholascathedral.co.uk\"><img src=\"../../../../sncm/Images/logo.jpg\"><br>Main Cathedral Website</a></p></td><td colspan=\"2\"><img src=\"".$this->headerImage."\" width=\"900\" /></td></tr>\n";
		$html .= "<tr><td colspan=\"3\"><hr></td></tr>\n";
		$html .= "<tr><td align=\"right\" width=\"150\">".$this->getMenuHTML()."</td>\n";

		return $html;
	}

	protected function getLogon()
	{
		$html = "<td>";
		$html .= "<center><h1>Please Log On</h1>";
		$html .= "<form action=\"home.php\" method=\"post\">";
		$html .= "<input type=\"hidden\" name=\"Action\"  value=\"members\" />";
		$html .= "<p>Username: </p>";
		$html .= "<input type=\"text\" name=\"logonUsername\" />";
		$html .= "<br />";
		$html .= "<p>Password: </p>";
		$html .= "<input type=\"password\" name=\"logonPassword\" />";
		$html .= "<br />";
		$html .= "<input type=\"submit\" value=\"Submit\" />";
		$html .= "</form></center>";
		$html .= "</td>";
		return $html;
	}
	
	protected function getContents()
	{
//		echo "Content = ".$this->contentHTML;
/*		if (isset($this->contentHTML))
			return "<td>".file_get_contents($this->contentHTML)."</td>";
		elseif (isset($this->contentClass))
		{
			//this is a bodge for members only at the mo
			if ($this->contentClass=="PanelEvents")
				$inset = new PanelEvents();
			if ($this->contentClass=="PanelRecitals")
				$inset = new PanelRecitals();
			if ($this->contentClass=="PanelMembers")
				$inset = new PanelMembers();
			if ($this->contentClass=="PanelChoirs")
				$inset = new PanelChoirs();
			$inset->setParentPage($this);
			return "<td>".$inset->getHTML()."</td>";
		}
		*/
		$html = "<td><h1>Home Page</h1>\n<p>This is the point of entry to the Cathedral Music Resources website. Select the option you wish.</p>";
		
		$sql = "SELECT m.CMRUserID, m.CMRUserGroupID, u.CMRUserID, g.GroupName
				FROM 
				(CMRUserGroupMembers AS m
				INNER JOIN CMRUsers AS u ON m.CMRUserID = u.CMRUserID)
				INNER JOIN CMRUserGroups as g ON g.CMRUserGroupID = m.CMRUserGroupID
				WHERE u.CMRUserID = ".$this->UserID;
		
		$this->Database->ExecuteSQL($sql);
		$row = $this->Database->getRow();
		
		$html .= "sessionID -> ".$this->SessionID."<br>User -> ".$this->User."<br>UserID -> ".$this->UserID."<br>Group -> ".$row['GroupName']."</td>";
		return $html;

	}

	protected function getBodyBottomHTML()
	{
		$html = "</td></tr>\n";
		$html .= "<tr><td colspan=\"2\"><hr>Cathedral Organists Association</td><td>Version 1.1.0 24/11/2010</td></tr>\n";
	
		$html .= "</table>\n";
		$html .= "</form>\n";
		$html .= "</body>\n";
		$html .= "</html>\n";
		return $html;
	}


	public function setDatabase($aDatabase)
	{
		$this->Database = $aDatabase;
	}

	public function getDatabase()
	{
		return $this->Database;
	}
	

	public function setAuthenticator($anAuthenticator)
	{
		$this->Authenticator = $anAuthenticator;
	}

	public function getAuthenticator()
	{
		return $this->Authenticator;
	}

	
	public function isAuthenticated()
	{
		if(!strcmp($this->Action,"logoff"))
			return(false);
			
		if(!isset($this->Authenticator))
		{
//			echo "no authent";
			if(isset($_POST['User']))
				$this->User = $_POST['User'];
			if(isset($_POST['SessionID']))
				$this->SessionID = $_POST['SessionID'];
			return true;
		}
	
		try
		{
//			echo "hello";
			if(isset($_POST['SessionID'])&&($_POST['SessionID']!=''))
			{
//				echo "boo".$_POST['SessionID'].$_POST['User'].$_POST['UserID'];
				if($this->Authenticator->isLoggedOn($_POST['SessionID'],$_POST['User']))
				{
					$this->User = $_POST['User'];
					$this->SessionID = $_POST['SessionID'];
					$this->UserID = $this->Authenticator->getUserID();
					return true;
				}
				else
				{
	//				$this->Authenticator->displayLogOn();
					return false;
				}
			}
			if(isset($_POST['logonUsername']))
			{
//				echo "booy".$_POST['logonUsername'];
				if ($this->Authenticator->tryLogOn(
//					$_POST['group'],
					"",
					$_POST['logonUsername'],
					$_POST['logonPassword']
					))
				{
					$this->User = $this->Authenticator->getUser();
					$this->UserID = $this->Authenticator->getUserID();
					$this->SessionID = $this->Authenticator->getSessionID();
//					echo "sessionID = ".$this->Authenticator->getSessionID();
					return true;
				}
				else
				{
//					$this->Authenticator->displayLogOn();
					return false;
				}
			}
		}
		catch(Exception $error){echo $error;} // 
//		$this->Authenticator->displayLogOn();
		return false;
	}	


	public function setStyleSheet($aStyleSheet)
	{
		$this->StyleSheet = $aStyleSheet;
	}


}

?>